/*
 * Copyright (C) Michael Opitz
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA
 */
#ifndef MULTICASTER_H
#define MULTICASTER_H

#include <string>
#include <list>
#include <utility>

#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>

#include "../model/dom.h"

#include "../core/null_deleter.h"

#include "../network/thread_helper.h"
#include "../network/worker_thread.h"
#include "event_handler.h"
#include "../network/listener.h"
#include "../network/notifier.h"
#include "../network/network.h"
#include "event_worker.h"

using namespace std;

namespace handler {
	class event_worker;
}

/** 
 * \brief This namespace includes all general purpose handler-functions
 * \author Michael Opitz
 */
namespace handler {
	/** 
	 * \brief This class implements the multicaster, at which handlers can
	 * register themselves. 
	 */
	class multicaster : public network::listener {
	public:
		struct event_storage {
			network::event_type type;
			boost::shared_ptr<network::i_socket> socket;
			boost::shared_ptr<handler::event_handler> handler;
		};
		typedef boost::shared_ptr<network::event_handler> 
			event_ptr;

		/** 
		 * \brief multicaster creates the multicaster. Don't use this
		 * constuctor directly, call create-instead.
		 * 
		 * \param q is the message_queue which should be used.
		 * \param num_threads is the number of worker threads. default:
		 * 5
		 */
		multicaster(boost::shared_ptr<network::message_queue> q, 
			int num_threads = 5);
		virtual ~multicaster();

		/** 
		 * \brief create creates the multicaster
		 * \see multicaster
		 */
		static boost::shared_ptr<handler::multicaster> create(
			boost::shared_ptr<network::message_queue> q, 
			int num_threads = 5);

		/** 
		 * \brief dispatch dispatches a received message. This method is
		 * called by worker_thread threads.
		 * 
		 * \param msg is a message from the message_queue.
		 */
		void dispatch(boost::shared_ptr<network::message> msg);

		/** 
		 * \brief add_handler adds a new handler to the multicaster.
		 * 
		 * \param t is the type, from which the handler listenses
		 * \param sock is the socket from which the handler will get
		 * messages
		 * \param h is the handler.
		 */
		void add_handler(network::event_type t, 
			boost::shared_ptr<network::i_socket> sock,
			boost::shared_ptr<handler::event_handler> h);
		/** 
		 * \brief remove_handler removes a handler from the multicaster
		 */
		void remove_handler(
			network::event_type t,
			boost::shared_ptr<network::i_socket> sock,
			boost::shared_ptr<handler::event_handler> h);

		/** 
		 * \brief notify is called by handler, if they want to delete
		 * themselves, if e.g. a connection was closed.
		 * 
		 * \param n is the notifier (event_handler).
		 */
		virtual void notify(const network::notifier *n);
	private:
		list<event_storage> _handler;
		boost::mutex _monitor;

		boost::shared_ptr<network::message_queue> _message_q;
		list<boost::shared_ptr<handler::event_worker> > _workers;
		boost::weak_ptr<handler::multicaster> _this;
	};
}

#endif
